package com.fyy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fyy.base.exception.XCException;
import com.fyy.base.model.PageParams;
import com.fyy.base.model.PageResult;
import com.fyy.content.mapper.CourseBaseMapper;
import com.fyy.content.mapper.CourseCategoryMapper;
import com.fyy.content.mapper.CourseMarketMapper;
import com.fyy.content.model.dto.AddCourseDto;
import com.fyy.content.model.dto.CourseBaseInfoDTO;
import com.fyy.content.model.dto.QueryCourseParamsDTO;
import com.fyy.content.model.dto.UpdateCourseDTO;
import com.fyy.content.model.po.CourseBasePO;
import com.fyy.content.model.po.CourseCategoryPO;
import com.fyy.content.model.po.CourseMarketPO;
import com.fyy.content.service.CourseBaseInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author fyy
 * @version 1.0
 * @description
 * @date 2024/7/8 0:33
 */
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {

    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    public PageResult<CourseBasePO> queryCourseBaseList(PageParams pageParams, QueryCourseParamsDTO queryCourseParamsDTO) {
        LambdaQueryWrapper<CourseBasePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(queryCourseParamsDTO.getCourseName()), CourseBasePO::getName, queryCourseParamsDTO.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDTO.getAuditStatus()), CourseBasePO::getAuditStatus, queryCourseParamsDTO.getAuditStatus());

        Page<CourseBasePO> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<CourseBasePO> pageResult = courseBaseMapper.selectPage(page, queryWrapper);
        List<CourseBasePO> courseBasePOList = pageResult.getRecords();
        long total = pageResult.getTotal();
        PageResult<CourseBasePO> courseBasePOPageResult = new PageResult<>(courseBasePOList, total, pageParams.getPageNo(), pageParams.getPageSize());
        return courseBasePOPageResult;
    }

    @Transactional
    public CourseBaseInfoDTO createCourseBase(Long companyId, AddCourseDto addCourseDto) {
        if (StringUtils.isBlank(addCourseDto.getName())) {
            XCException.cast("课程名称为空");
        }

        if (StringUtils.isBlank(addCourseDto.getMt())) {
            throw new RuntimeException("课程分类为空");
        }

        if (StringUtils.isBlank(addCourseDto.getSt())) {
            throw new RuntimeException("课程分类为空");
        }

        if (StringUtils.isBlank(addCourseDto.getGrade())) {
            throw new RuntimeException("课程等级为空");
        }

        if (StringUtils.isBlank(addCourseDto.getTeachmode())) {
            throw new RuntimeException("教育模式为空");
        }

        if (StringUtils.isBlank(addCourseDto.getUsers())) {
            throw new RuntimeException("适应人群为空");
        }

        if (StringUtils.isBlank(addCourseDto.getCharge())) {
            throw new RuntimeException("收费规则为空");
        }

        CourseBasePO courseBaseNew = new CourseBasePO();
        BeanUtils.copyProperties(addCourseDto, courseBaseNew);
        //设置审核状态
        courseBaseNew.setAuditStatus("202002");
        //设置发布状态
        courseBaseNew.setStatus("203001");
        //机构id
        courseBaseNew.setCompanyId(companyId);
        //添加时间
        courseBaseNew.setCreateDate(LocalDateTime.now());
        //插入课程基本信息表
        int insert = courseBaseMapper.insert(courseBaseNew);
        if (insert <= 0) {
            throw new RuntimeException("新增课程基本信息失败");
        }

        //向课程营销表保存课程营销信息
        CourseMarketPO courseMarketNew = new CourseMarketPO();
        Long courseId = courseBaseNew.getId();
        BeanUtils.copyProperties(addCourseDto, courseMarketNew);
        courseMarketNew.setId(courseId);
        int i = saveCourseMarket(courseMarketNew);
        if (i < 1) {
            throw new RuntimeException("保存课程营销信息失败");
        }

        //查询课程基本信息及营销信息并返回
        return getCourseBaseInfo(courseId);
    }

    //根据课程id查询课程基本信息，包括基本信息和营销信息
    public CourseBaseInfoDTO getCourseBaseInfo(Long courseId) {
        CourseBasePO courseBasePO = courseBaseMapper.selectById(courseId);
        if (courseBasePO == null) {
            return null;
        }
        CourseMarketPO courseMarketPO = courseMarketMapper.selectById(courseId);
        CourseBaseInfoDTO courseBaseInfoDTO = new CourseBaseInfoDTO();
        BeanUtils.copyProperties(courseBasePO, courseBaseInfoDTO);
        if (courseMarketPO != null) {
            BeanUtils.copyProperties(courseMarketPO, courseBaseInfoDTO);
        }

        //查询分类名称
        CourseCategoryPO courseCategoryBySt = courseCategoryMapper.selectById(courseBasePO.getSt());
        courseBaseInfoDTO.setStName(courseCategoryBySt.getName());
        CourseCategoryPO courseCategoryByMt = courseCategoryMapper.selectById(courseBasePO.getMt());
        courseBaseInfoDTO.setMtName(courseCategoryByMt.getName());
        return courseBaseInfoDTO;
    }

    @Transactional
    public CourseBaseInfoDTO updateCourseBaseInfo(Long companyId, UpdateCourseDTO updateCourseDTO) {
        Long courseId = updateCourseDTO.getId();
        CourseBasePO courseBasePO = courseBaseMapper.selectById(courseId);
        if (courseBasePO == null) {
            XCException.cast("课程不存在");
        }

        if (!courseBasePO.getCompanyId().equals(companyId)) {
            XCException.cast("无法修改其他机构的课程");
        }

        BeanUtils.copyProperties(updateCourseDTO, courseBasePO);
        courseBasePO.setChangeDate(LocalDateTime.now());
        int i = courseBaseMapper.updateById(courseBasePO);

        CourseMarketPO courseMarketPO = new CourseMarketPO();
        BeanUtils.copyProperties(updateCourseDTO, courseMarketPO);
        saveCourseMarket(courseMarketPO);
        CourseBaseInfoDTO courseBaseInfoDTO = getCourseBaseInfo(courseId);
        return courseBaseInfoDTO;
    }

    private int saveCourseMarket(CourseMarketPO courseMarketNew) {
        String charge = courseMarketNew.getCharge();
        if (StringUtils.isBlank(charge)) {
            XCException.cast("收费规则没有选择");
        }

        //收费规则为收费
        if (charge.equals("201001")) {
            if (courseMarketNew.getPrice() == null || courseMarketNew.getPrice().floatValue() <= 0) {
                XCException.cast("课程为收费价格不能为空且必须大于0");
            }
        }

        //根据id从课程营销表查询
        CourseMarketPO courseMarketObj = courseMarketMapper.selectById(courseMarketNew.getId());
        if (courseMarketObj == null) {
            return courseMarketMapper.insert(courseMarketNew);
        } else {
            BeanUtils.copyProperties(courseMarketNew, courseMarketObj);
            courseMarketObj.setId(courseMarketNew.getId());
            return courseMarketMapper.updateById(courseMarketObj);
        }
    }
}
